// import 'package:flutter/material.dart';
// import 'package:shared_preferences/shared_preferences.dart';
//
// class ChangePasswordPage extends StatefulWidget {
//   const ChangePasswordPage({super.key});
//
//   @override
//   State<ChangePasswordPage> createState() => _ChangePasswordPageState();
// }
//
// class _ChangePasswordPageState extends State<ChangePasswordPage> {
//   final TextEditingController _qqController = TextEditingController();
//   final TextEditingController _oldPasswordController = TextEditingController();
//   final TextEditingController _newPasswordController = TextEditingController();
//   final TextEditingController _confirmNewPasswordController =
//   TextEditingController();
//
//   Future<void> _changePassword() async {
//     final prefs = await SharedPreferences.getInstance();
//     final qqNumber = _qqController.text.trim();
//     final oldPassword = _oldPasswordController.text.trim();
//     final newPassword = _newPasswordController.text.trim();
//     final confirmNewPassword = _confirmNewPasswordController.text.trim();
//
//     if (qqNumber.isEmpty ||
//         oldPassword.isEmpty ||
//         newPassword.isEmpty ||
//         confirmNewPassword.isEmpty) {
//       ScaffoldMessenger.of(context).showSnackBar(
//         const SnackBar(content: Text('请填写所有信息')),
//       );
//       return;
//     }
//
//     final storedPassword = prefs.getString('qq_$qqNumber');
//     if (storedPassword != oldPassword) {
//       ScaffoldMessenger.of(context).showSnackBar(
//         const SnackBar(content: Text('旧密码不正确')),
//       );
//       return;
//     }
//
//     if (newPassword != confirmNewPassword) {
//       ScaffoldMessenger.of(context).showSnackBar(
//         const SnackBar(content: Text('新密码与确认密码不匹配')),
//       );
//       return;
//     }
//
//     // 更新密码
//     prefs.setString('qq_$qqNumber', newPassword);
//     ScaffoldMessenger.of(context).showSnackBar(
//       const SnackBar(content: Text('密码修改成功')),
//     );
//     Navigator.pop(context); // 返回登录页面
//   }
//
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         title: const Text('修改密码'),
//         backgroundColor: Colors.blue,
//       ),
//       body: Padding(
//         padding: const EdgeInsets.all(16.0),
//         child: Column(
//           children: [
//             TextField(
//               controller: _qqController,
//               decoration: const InputDecoration(labelText: '输入QQ号', border: OutlineInputBorder()),
//               keyboardType: TextInputType.number,
//             ),
//             const SizedBox(height: 11),
//             TextField(
//               controller: _oldPasswordController,
//               decoration: const InputDecoration(labelText: '输入旧密码', border: OutlineInputBorder()),
//               obscureText: true,
//             ),
//             const SizedBox(height: 11),
//             TextField(
//               controller: _newPasswordController,
//               decoration: const InputDecoration(labelText: '输入新密码', border: OutlineInputBorder()),
//               obscureText: true,
//             ),
//             const SizedBox(height: 11),
//             TextField(
//               controller: _confirmNewPasswordController,
//               decoration: const InputDecoration(labelText: '确认新密码', border: OutlineInputBorder()),
//               obscureText: true,
//             ),
//             const SizedBox(height: 17),
//             ElevatedButton(
//               style: ElevatedButton.styleFrom(
//                 backgroundColor: Colors.blue,
//               ),
//               onPressed: _changePassword,
//               child: const Text('确认修改', style: TextStyle(color: Colors.white)),
//             ),
//           ],
//         ),
//       ),
//     );
//   }
// }


import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ChangePasswordPage extends StatefulWidget {
  const ChangePasswordPage({super.key});

  @override
  State<ChangePasswordPage> createState() => _ChangePasswordPageState();
}

class _ChangePasswordPageState extends State<ChangePasswordPage> {
  final TextEditingController _qqController = TextEditingController();
  final TextEditingController _oldPasswordController = TextEditingController();
  final TextEditingController _newPasswordController = TextEditingController();
  final TextEditingController _confirmNewPasswordController = TextEditingController();

  /// 密码校验正则：至少8位，包含数字和字母
  final RegExp passwordRegExp = RegExp(r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$');

  Future<void> _changePassword() async {
    final prefs = await SharedPreferences.getInstance();
    final qqNumber = _qqController.text.trim();
    final oldPassword = _oldPasswordController.text.trim();
    final newPassword = _newPasswordController.text.trim();
    final confirmNewPassword = _confirmNewPasswordController.text.trim();

    if (qqNumber.isEmpty ||
        oldPassword.isEmpty ||
        newPassword.isEmpty ||
        confirmNewPassword.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('请填写所有信息')),
      );
      return;
    }

    final storedPassword = prefs.getString('qq_$qqNumber');
    if (storedPassword != oldPassword) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('旧密码不正确')),
      );
      return;
    }

    if (!passwordRegExp.hasMatch(newPassword)) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('新密码必须至少8位，并包含数字和字母')),
      );
      return;
    }

    if (newPassword != confirmNewPassword) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('新密码与确认密码不匹配')),
      );
      return;
    }

    // 更新密码
    await prefs.setString('qq_$qqNumber', newPassword);
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('密码修改成功')),
    );
    Navigator.pop(context); // 返回登录页面
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('修改密码'),
        backgroundColor: Colors.blue,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              TextField(
                controller: _qqController,
                decoration: const InputDecoration(
                  labelText: '输入QQ号',
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.number,
              ),
              const SizedBox(height: 11),
              TextField(
                controller: _oldPasswordController,
                decoration: const InputDecoration(
                  labelText: '输入旧密码',
                  border: OutlineInputBorder(),
                ),
                obscureText: true,
              ),
              const SizedBox(height: 11),
              TextField(
                controller: _newPasswordController,
                decoration: const InputDecoration(
                  labelText: '输入新密码',
                  border: OutlineInputBorder(),
                ),
                obscureText: true,
              ),
              const SizedBox(height: 11),
              TextField(
                controller: _confirmNewPasswordController,
                decoration: const InputDecoration(
                  labelText: '确认新密码',
                  border: OutlineInputBorder(),
                ),
                obscureText: true,
              ),
              const SizedBox(height: 17),
              const Text(
                '· 密码修改成功后，请重新登录',
                // style: TextStyle(color: Colors.red),
              ),
              const Text(
                '· 新密码必须至少8位，并包含数字和字母',
                // style: TextStyle(color: Colors.red),
              ),
              const SizedBox(height: 10),
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue,
                ),
                onPressed: _changePassword,
                child: const Text('确认修改', style: TextStyle(color: Colors.white)),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
